//===============================================================================
// Microsoft Architecture Strategy Team
// LitwareHR - SaaS Sample Application
//===============================================================================
// Copyright  Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================


namespace LitwareHR.PublicApp.Presenters.Careers
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using LitwareHR.Portfolio.Gateways;
    using LitwareHR.Recruiting.Contract;
    using Shp.Runtime.Contract;
using System.Collections.ObjectModel;
    using LitwareHR.PublicApp.Presenters.Properties;

    public class SearchResumesPresenter
    {
        private ISearchResumesView View { get; set; }
        public IEnumerable<EntityField> CustomFields { get; set; }
        public Collection<PropertyFilter> CustomFilters { get; set; }        

        public void AddCustomFilter(string fieldName, string fieldValue, LogicalConnector op)
        {
            EntityField entity = CustomFields.First(e => e.Name == fieldName);
            PropertyType type = PropertyType.String;
            if (entity.TypeName == "Date and time")
            {
                type = PropertyType.DateTime;
            }
            else if (entity.TypeName == "Boolean (Yes | No)")
            {
                type = PropertyType.Boolean;
            }

            PropertyFilter filter = new PropertyFilter(fieldName, type, LogicalOperator.Equal, fieldValue, op);
            if (CustomFilters == null)
            {
                CustomFilters = new Collection<PropertyFilter>();
            }
            CustomFilters.Add(filter);
            View.ShowCustomFilters(CustomFilters);
        }

        public void ClearCustomFilters()
        {
            if (CustomFilters != null)
            {
                CustomFilters.Clear();
                View.ShowCustomFilters(CustomFilters);
            }            
        }

        public SearchResumesPresenter(ISearchResumesView view)
        {
            View = view;
        }

        public void OnViewLoaded(string tenantAlias)
        {            
            EntityField[] positionFields = DatamodelGateway.GetEntityDefFields(tenantAlias, Constants.Entity.Position);
            CustomFields = positionFields.Where(x => x.IsVisibleFromApplicationForm);
            View.SetCustomFields(CustomFields);
            View.SetDetailsLink("Evaluation/" + Resources.ViewResumeDetailsUrlFormat);
        }

        public void OnSearch()
        {
            PropertyFilter[] filters = GenerateFilters();
            Resume[] localTenantResumes = RecruitingGateway.SearchResumes(filters);

			View.ShowLocalTenantResumeResult(localTenantResumes);

            if (View.CrossTenant)
            {
                Resume[] crossTenantResumes = RecruitingGateway.SearchCrossTenantResumes(filters);
                View.ShowCrossTenantResumeResult(crossTenantResumes);
            }
            else
            {
                View.HideCrossTenantResults();
            }
        }

        private PropertyFilter[] GenerateFilters()
        {
            List<PropertyFilter> filters = new List<PropertyFilter>();

            if (!string.IsNullOrEmpty(View.Address))
            {
                filters.Add(new PropertyFilter("Address", PropertyType.String, LogicalOperator.Equal, View.Address, LogicalConnector.And));
            }
            if (View.DateFrom!=DateTime.MinValue)
            {
                filters.Add(new PropertyFilter("Date", PropertyType.DateTime, LogicalOperator.GreaterOrEqualThan, View.DateFrom, LogicalConnector.And));
            }
            if (View.DateTo!=DateTime.MaxValue)
            {
                filters.Add(new PropertyFilter("Date", PropertyType.DateTime, LogicalOperator.LessOrEqualThan, View.DateTo, LogicalConnector.And));
            }

            if (CustomFilters != null)
            {
                foreach (PropertyFilter filter in CustomFilters)
                {
                    filters.Add(filter);
                }
            }

            return filters.ToArray();
        }
    }
}
